package com.itheima.web.servlet; /**
 * @author Xiao
 * @date 2021/9/8
 */

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/deleteByIdsServlet")
public class DeleteByIdsServlet extends HttpServlet {
    private final BrandServiceImpl brandService = new BrandServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String jsonBrand = request.getReader().readLine();
        List<Brand> list = JSON.parseObject(jsonBrand, List.class);
        int[] ids = new int[list.size()];
        for (int i = 0; i < ids.length; i++) {
            Brand brand = list.get(i);
            ids[i] = brand.getId();
        }
        brandService.deleteByIds(ids);
        response.getWriter().write("success");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
